<template>
  <div id="userManage">
    <!--搜索框-->
    <a-form layout="inline" :model="searchParam" @finish="doSearch" class="searchForm">
      <a-form-item label="用户名">
        <a-input v-model:value="searchParam.username" placeholder="用户名" allowClear />
      </a-form-item>
      <a-form-item label="昵称">
        <a-input v-model:value="searchParam.nickname" placeholder="昵称" allowClear />
      </a-form-item>
      <a-form-item label="角色">
        <a-select ref="select" v-model:value="searchParam.userRole" style="width: 120px" allowClear>
          <a-select-option value="user">普通用户</a-select-option>
          <a-select-option value="admin">管理员</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit">搜索</a-button>
      </a-form-item>
    </a-form>
    <a-table
      :columns="columns"
      :data-source="userData"
      bordered
      :pagination="pagination"
      @change="pageSizeChange"
    >
      <template #bodyCell="{ column, record }">
        <!--头像的展示方式-->
        <template v-if="column.dataIndex === 'avatar'">
          <a-image :src="record.avatar" :width="100" />
        </template>
        <!--用户角色展示方式-->
        <template v-else-if="column.dataIndex === 'userRole'">
          <div v-if="record.userRole === 'admin'">
            <a-tag color="green">管理员</a-tag>
          </div>
          <div v-else>
            <a-tag color="blue">普通用户</a-tag>
          </div>
        </template>
        <template v-else-if="column.key === 'action'">
          <a-button danger size="middle" @click="doDelete(record.id)">删除</a-button>
        </template>
      </template>
    </a-table>
  </div>
</template>
<script lang="ts" setup>
import { computed, onMounted, reactive, ref } from 'vue'
import { deleteUserByIdUsingDelete, getUserVoPageUsingGet } from '@/api/userController.ts'
import { message } from 'ant-design-vue'

const columns = [
  {
    title: '用户名',
    dataIndex: 'username',
  },
  {
    title: '昵称',
    dataIndex: 'nickname',
  },
  {
    title: '头像',
    dataIndex: 'avatar',
  },
  {
    title: '用户简介',
    dataIndex: 'userDesc',
  },
  {
    title: '用户角色',
    dataIndex: 'userRole',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
  {
    title: '操作',
    key: 'action',
  },
]
// 用户列表
const userData = ref<API.UserVO[]>([])
const total = ref(0)
// 搜索条件
const searchParam = reactive<API.getUserVOPageUsingGETParams>({
  pageNum: 1,
  pageSize: 20,
  username: '',
  nickname: '',
  userRole: '',
})

// 获取用户数据
const queryUserPage = async () => {
  const res = await getUserVoPageUsingGet({
    ...searchParam,
  })
  userData.value = res.data.records ?? []
  total.value = res.data.total ?? 0
}
// 分页器
const pageSizeOptions = ref<string[]>(['1', '2', '10', '20', '50'])
const pageSizeChange = (page) => {
  searchParam.pageNum = page.current
  searchParam.pageSize = page.pageSize
  queryUserPage()
}
const pagination = computed(() => {
  return {
    current: searchParam.pageNum,
    pageSize: searchParam.pageSize,
    total: total.value,
    showSizeChanger: true, // 显示可改变分页大小
    showTotal: (total: number) => `共${total}条`,
    pageSizeOptions: pageSizeOptions.value,
  }
})
const doSearch = () => {
  // 重置页码
  searchParam.pageNum = 1
  queryUserPage()
}
const doDelete = async (id: string) => {
  if (id) {
    await deleteUserByIdUsingDelete({ userId: id })
    message.success('操作成功')
    queryUserPage()
  }
}
onMounted(() => {
  queryUserPage()
})
</script>

<style scoped>
.searchForm {
  margin-bottom: 16px;
}
</style>
